1
/********************************** Module Header **************************************************\
2 * Module Name: ConditionLine.cs
3 * Project: CSDynamicallyBuildLambdaExpressionWithField
4 * Copyright (c) Microsoft Corporation.
6 * The ConditionLine.cs file defines some sub-condition connection operators and some property boxes.
8 * This source is subject to the Microsoft Public License.
9 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 * All other rights reserved.
12 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
13 * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 \**************************************************************************************************/
17 using System
.Collections
;
18 using System
.Collections
.Generic
;
19 using System
.Reflection
;
21 namespace DynamicCondition
23 internal partial class ConditionLine
26 internal ConditionLine()
28 InitializeComponent();
34 private Type _dataType
;
48 /// Property DataSource
50 private PropertyInfo
[] _dataSource
;
51 public PropertyInfo
[] DataSource
60 cmbColumn
.DataSource
= value;
61 cmbColumn
.DisplayMember
= "Name";
66 /// Condition compare operator
68 public DynamicCondition
.DynamicQuery
.Condition
.Compare OperatorType
72 return ((lb
.Text
== "AND") ? DynamicCondition
.DynamicQuery
.Condition
.Compare
.And
:
73 DynamicQuery
.Condition
.Compare
.Or
);
77 if (value != DynamicQuery
.Condition
.Compare
.And
& value != DynamicQuery
.Condition
.Compare
.Or
)
79 throw new ArgumentException("OperatorType must be \"And\" or \"Or\"");
81 lb
.Text
= value.ToString().ToUpper();
86 /// Returns a Condition(Of T) which represents the criteria stored in the UserControl
88 public DynamicCondition
.DynamicQuery
.Condition
<T
> GetCondition
<T
>(T dataSrc
)
91 var pType
= ((PropertyInfo
)cmbColumn
.SelectedItem
).PropertyType
;
93 // CheckType ensures that T and T? are treated the same
94 if (CheckType
<bool>(pType
))
96 return MakeCond(dataSrc
, pType
, chkValue
.Checked
);
99 else if (CheckType
<DateTime
>(pType
))
101 return MakeCond(dataSrc
, pType
, dtpValue
.Value
);
103 else if (CheckType
<char>(pType
))
105 return MakeCond(dataSrc
, pType
, Convert
.ToChar(tbValue
.Text
));
107 else if (CheckType
<long>(pType
))
109 return MakeCond(dataSrc
, pType
, Convert
.ToInt64(tbValue
.Text
));
111 else if (CheckType
<short>(pType
))
113 return MakeCond(dataSrc
, pType
, Convert
.ToInt16(tbValue
.Text
));
115 else if (CheckType
<ulong>(pType
))
117 return MakeCond(dataSrc
, pType
, Convert
.ToUInt64(tbValue
.Text
));
119 else if (CheckType
<ushort>(pType
))
121 return MakeCond(dataSrc
, pType
, Convert
.ToUInt16(tbValue
.Text
));
123 else if (CheckType
<float>(pType
))
125 return MakeCond(dataSrc
, pType
, Convert
.ToSingle(tbValue
.Text
));
127 else if (CheckType
<double>(pType
))
129 return MakeCond(dataSrc
, pType
, Convert
.ToDouble(tbValue
.Text
));
131 else if (CheckType
<decimal>(pType
))
133 return MakeCond(dataSrc
, pType
, Convert
.ToDecimal(tbValue
.Text
));
135 else if (CheckType
<int>(pType
))
137 return MakeCond(dataSrc
, pType
, Convert
.ToInt32(SimulateVal
.Val(tbValue
.Text
)));
139 else if (CheckType
<uint>(pType
))
141 return MakeCond(dataSrc
, pType
, Convert
.ToUInt32(tbValue
.Text
));
144 // This can only ever be String, since we filtered the types that we added to the ComboBox
147 return MakeCond(dataSrc
, pType
, tbValue
.Text
);
152 public static List
<Type
> typeList
;
155 /// contract behind the where keyword
157 public static List
<Type
> GetSupportedTypes()
159 if (typeList
== null)
161 typeList
= new List
<Type
>();
162 typeList
.AddRange(new Type
[] {typeof(DateTime
), typeof(DateTime
?), typeof(char),
163 typeof(char?), typeof(long), typeof(long?), typeof(short), typeof(short?),
164 typeof(ulong), typeof(ulong?), typeof(ushort), typeof(ushort?), typeof(float),
165 typeof(float?), typeof(double), typeof(double?), typeof(decimal), typeof(decimal?),
166 typeof(bool), typeof(bool?), typeof(int), typeof(int?), typeof(uint), typeof(uint?),
174 /// Combine condition
176 private void ConditionLine_Load(object sender
, EventArgs e
)
178 cmbOperator
.DisplayMember
= "Name";
179 cmbOperator
.ValueMember
= "Value";
180 var opList
= MakeList(new { Name = "Equal", Value = DynamicQuery.Condition.Compare.Equal }
,
181 new { Name = "Not Equal", Value = DynamicQuery.Condition.Compare.NotEqual }
,
182 new { Name = ">", Value = DynamicQuery.Condition.Compare.GreaterThan }
,
183 new { Name = ">=", Value = DynamicQuery.Condition.Compare.GreaterThanOrEqual }
,
184 new { Name = "<", Value = DynamicQuery.Condition.Compare.LessThan }
,
185 new { Name = "<=", Value = DynamicQuery.Condition.Compare.LessThanOrEqual }
,
186 new { Name = "Like", Value = DynamicQuery.Condition.Compare.Like }
);
187 cmbOperator
.DataSource
= opList
;
191 /// select which control to demonstrate when get property from the user picked
193 private void cboColumn_SelectedIndexChanged(object sender
, EventArgs e
)
196 // Get the underlying type for the property the user picked
197 var propType
= ((PropertyInfo
)cmbColumn
.SelectedItem
).PropertyType
;
199 // Display appropriate control (CheckBox/TextBox/DateTimePicker) for property type
200 if (CheckType
<bool>(propType
))
202 SetVisibility(true, false, false);
205 else if (CheckType
<DateTime
>(propType
))
207 SetVisibility(false, true, false);
211 SetVisibility(false, false, true);
216 /// Set which control is visible
218 private void SetVisibility(bool chkBox
, bool datePicker
, bool txtBox
)
220 chkValue
.Visible
= chkBox
;
221 tbValue
.Visible
= txtBox
;
222 dtpValue
.Visible
= datePicker
;
226 /// Toggle between AND/OR
228 private void lblOperator_Click(object sender
, System
.EventArgs e
)
230 lb
.Text
= ((lb
.Text
== "AND") ? "OR" : "AND");
234 /// MakeCond Operator
236 private DynamicCondition
.DynamicQuery
.Condition
<T
> MakeCond
<T
, S
>(T dataSource
, Type propType
, S
value)
238 IEnumerable
<T
> dataSourceType
= null;
239 return DynamicCondition
.DynamicQuery
.Condition
.Create
<T
>(dataSourceType
, cmbColumn
.Text
,
240 (DynamicQuery
.Condition
.Compare
)cmbOperator
.SelectedValue
, value, propType
);
244 /// Returns true if propType is of type T or Nullable(Of T)
246 private static bool CheckType
<T
>(Type propType
) where T
: struct
248 return (propType
.Equals(typeof(T
)) | propType
.Equals(typeof(T
?)));
252 /// Turns list of parameters into an IEnumerable(Of T) (where T is an anonymous type in this case)
254 private static IEnumerable
<T
> MakeList
<T
>(params T
[] items
)